package com.kingtown.inventory;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.hutool.json.JSONUtil;
import com.google.gson.reflect.TypeToken;
import com.kingtown.inventory.domain.TaskItem;
import com.kingtown.inventory.domain.bo.SubmitBo;
import com.kingtown.inventory.domain.dto.TaskDetailItemDto;
import com.kingtown.inventory.net.NetWorkManager;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.stream.Collectors;

public class TaskDetailActivity extends AppCompatActivity implements View.OnClickListener {
    Context _this;
    MainApplication mApplication;

    TaskItem taskItem;

    private Switch mSwitch;

    Handler handler;

    TextView totalQuantity, totalWeight, totalGoldWeight, totalPrice, totalLaborCostsStr, surplusEmpty, totalCount, totalLabel, surplusCount;

    Timer updateStaticTimer;
    TimerTask updateStaticTask;

    Button btnShowSurplus, btnSubmit, btnStart;

    boolean isDoSomeThing = false;

    Menu mMenu;

    int pushTimes = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_detail);

        _this = this;
        mApplication = (MainApplication) this.getApplication();
        setTitle("盘点详情");

//        Bundle bundle = this.getIntent().getExtras();
//        taskItem = mApplication.gson.fromJson(bundle.getString("taskItem"), TaskItem.class);
//        Log.i("taskItem", taskItem.toString());


        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        stopTimer();
        if (mApplication.loopFlag) {
            mApplication.stopInventory();
        }
        super.onPause();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

        }
    }

    public void startTimer() {
        this.stopTimer();
        updateStaticTimer = new Timer();
        updateStaticTask = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(() -> {});
            }
        };
        this.updateStaticTimer.schedule(this.updateStaticTask, 0, 200);
    }

    public void stopTimer() {
        if (this.updateStaticTimer != null) {
            this.updateStaticTimer.cancel();
        }
    }

    @SuppressLint("CheckResult")
    private void initData() {
        mApplication.resetData();

    }


    private boolean start() {
        if (mApplication.startInventory()) {
            isDoSomeThing = true;
            startTimer();
            return true;
        } else {
            return false;
        }
    }

    private boolean stop() {
        if (mApplication.stopInventory()) {
            stopTimer();
            return true;
        } else {
            return false;
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == 139 || keyCode == 280 || keyCode == 293) {
            if (btnStart.getText().equals("开始")) {
                if (start()) {
                    btnStart.setText("停止");
                } else {
                    Toast.makeText(this, "开始盘点失败", Toast.LENGTH_SHORT).show();
                }
            } else {
                if (stop()) {
                    btnStart.setText("开始");
                } else {
                    Toast.makeText(this, "停止盘点失败", Toast.LENGTH_SHORT).show();
                }
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}